<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
      canvas {
        border: 1px solid red;
      }
    </style>
  </head>
  <body>
    <canvas width="500" height="500">
      您的浏览器版本过低，不支持canvas，请升级浏览器
    </canvas>
    <script>
      // 获取dom节点
      const canvas = document.querySelector("canvas");
      // 获取画笔
      const ctx = canvas.getContext("2d"); // 2d 表示 画的2d图
      canvas.onmousedown = function (event) {
        const mouseX = event.clientX;
        const mouseY = event.clientY;
        ctx.moveTo(mouseX, mouseY);
        canvas.onmousemove = function (e) {
          const mouseX = e.clientX;
          const mouseY = e.clientY;
          ctx.lineTo(mouseX, mouseY);
          ctx.stroke();
        };
      };
      document.onmouseup = function () {
        canvas.onmousemove=false;
      };
    </script>
  </body>
</html>
